package com.keyroy.game.puzzle_rpg.utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;

import com.keyroy.game.puzzle_rpg.views.trees.ItemSetTreeView;

public class UIUtil {

	public static final Object TREE_NODE_MAIN = 0;
	public static final Object TREE_NODE_SUB = 1;

	public static void center(Shell shell) {
		Rectangle displayBounds = shell.getDisplay().getPrimaryMonitor().getBounds();
		Rectangle shellBounds = shell.getBounds();
		int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1;
		int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1;
		shell.setLocation(x, y);
	}

	public static void open(Shell shell) {
		center(shell);
		shell.open();
	}

	public static Image getImage(String path) {
		return SWTResourceManager.getImage(UIUtil.class, path);
	}

	public static void removeAll(CTabFolder cTabFolder) {
		CTabItem[] cTabItems = cTabFolder.getItems();
		for (int i = 0; i < cTabItems.length; i++) {
			cTabItems[i].dispose();
		}
	}

	public static void removeAll(Composite composite) {
		Control[] controls = composite.getChildren();
		for (int i = 0; i < controls.length; i++) {
			controls[i].dispose();
		}
		composite.layout();
		composite.redraw();
	}

	public static Menu getMenu(Composite paret, MouseEvent e, String[] names, String[] icons, SelectionListener[] events) {
		Menu menu = new Menu(paret);
		for (int i = 0; i < names.length; i++) {
			if (names[i] != null) {
				MenuItem menuItem = new MenuItem(menu, SWT.NONE);
				menuItem.setImage(SWTResourceManager.getImage(ItemSetTreeView.class, icons[i]));
				menuItem.setText(names[i]);
				menuItem.addSelectionListener(events[i]);
			} else {
				new MenuItem(menu, SWT.SEPARATOR);
			}

		}

		org.eclipse.swt.graphics.Point point = paret.toDisplay(e.x, e.y);
		menu.setLocation(point.x, point.y);
		return menu;
	}

}
